﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace solitaire
{
    public partial class FinishPile : UserControl, ICardContainer
    {
        private Stack<Card> _cards = new Stack<Card>();
                
        public FinishPile()
        {
            InitializeComponent();
        }

        #region ICardContainer Members

        public Card RetrieveTopCard()
        {
            Card c = _cards.Pop();
            UpdateCardDisplay();
            if (GameStatus.Preferences.EnableVegasRules) GameStatus.BankRoll -= 5;
            return c;
        }

        public Card RetrieveCard(Card c)
        {
            throw new NotImplementedException();
        }

        public Card[] RetrieveCardAndDescendants(Card c)
        {
            Card nc = _cards.Pop().Clone();
            UpdateCardDisplay();
            if (GameStatus.Preferences.EnableVegasRules) GameStatus.BankRoll -= 5;
            return new Card[] { nc };
        }

        public void AddCard(Card c)
        {
            _cards.Push(c);
            c.FromContainer = this;
            UpdateCardDisplay();
            if (GameStatus.Preferences.EnableVegasRules) GameStatus.BankRoll += 5;
        }

        public bool IsInside(Point p)
        {
            if (p.X < Canvas.GetLeft(this) || p.X > Canvas.GetLeft(this) + this.Width) return false;
            if (p.Y < Canvas.GetTop(this) || p.Y > Canvas.GetTop(this) + this.Height) return false;
            return true;
        }

        private void UpdateCardDisplay()
        {
            ClearAllCardsFromUI();
            if (_cards.Count > 0)
            LayoutRoot.Children.Add(_cards.Peek());
        }

        public int TotalCards
        {
            get { return _cards.Count; }
        }

        public bool IsGoodCardCandidate(Card c)
        {
            if (_cards.Count > 0)
            {
                Card cc = _cards.Peek();
                if (cc.CardSuit != c.CardSuit) return false;
                if ((int)c.CardValue - (int)cc.CardValue != 79) return false;
            }
            else
            {
                if (c.CardValue != CardValue.Ace) return false;
            }
            return true;
        }

        public void Clear()
        {
            ClearAllCardsFromUI();
            _cards.Clear();
        }

        #endregion

        internal Card PeekTopCard()
        {
            return (_cards.Count > 0) ? _cards.Peek() : null;
        }

        private void ClearAllCardsFromUI()
        {
            List<Card> killList = new List<Card>();

            foreach (UIElement eu in LayoutRoot.Children)
            {
                if (eu is Card) killList.Add((Card)eu);
            }
            while (killList.Count > 0)
            {
                LayoutRoot.Children.Remove(killList[0]);
                killList.RemoveAt(0);
            }
        }
    }
}
